//=======================================================
//AUTHOR: JEFF XL. ZHANG ARIZONA STATE UNIVERSITY
//ALL RIGHTS RESERVED  2008
//=======================================================
#pragma once
//#ifndef		//potential replacement for pragma once
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <iostream>
#include "line.h"
#include "display.h"
#include "data_structure.h"

using namespace std;

class Interpolation{
public:
	double IntP(const double x, const double y, int channel) const;
	Interpolation(IplImage *aimg);
private:
	IplImage *_img;
	int Bound(double x) const;
};

Interpolation::Interpolation(IplImage *aimg){
	_img = cvCloneImage(aimg);
}

double Interpolation::IntP(double x, double y, int channel) const{		// Bilinear Interporlation.
	double value;
	double rx,ry,vi0,vi1;
	rx = x - floor(x); ry = y - floor(y);
	int x_f = floor(x),y_f = floor(y), x_c = ceil(x), y_c = ceil(y);
	if(Bound(y_f*_img->widthStep + x_f)&&Bound((y_f*_img->widthStep + x_c))&&Bound(y_c*_img->widthStep + x_f)&&Bound(y_c*_img->widthStep + x_c)){
//	if((img->imageData[y_f*img->widthStep + x_f])&&(img->imageData[y_f*img->widthStep + x_c]) &&(img->imageData[y_c*img->widthStep + x_f]) &&(img->imageData[y_c*img->widthStep + x_c])){
		vi0 = rx*(uchar)_img->imageData[y_f*_img->widthStep + x_f + channel] + (1-rx)*(uchar)_img->imageData[y_f*_img->widthStep + x_c + channel];
		vi1 = rx*(uchar)_img->imageData[y_c*_img->widthStep + x_f + channel] + (1-rx)*(uchar)_img->imageData[y_c*_img->widthStep + x_c + channel];
//	vi0 = rx*img->imageData[(int)floor(y)*img->widthStep + (int)floor(x)] + (1-rx)*img->imageData[(int)floor(y)*img->widthStep + (int)ceil(x)];
//	vi1 = rx*img->imageData[(int)ceil(y)*img->widthStep + (int)floor(x)] + (1-rx)*img->imageData[(int)ceil(y)*img->widthStep + (int)ceil(x)];
	value = ry*vi1 + (1 - ry)*vi0;
	return value;
	}
	else
		return 0;
}


int Interpolation::Bound(double x) const {
	int bound = ((x >= 0)&&( x <= _img->height*_img->widthStep - 1));
	return bound;
}
